package com.onemileshop.updater;

import java.io.BufferedInputStream;
import java.io.File;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.net.HttpURLConnection;
import java.net.MalformedURLException;
import java.net.URL;

import android.R.integer;
import android.app.Activity;
import android.app.ProgressDialog;
import android.content.Context;
import android.content.Intent;
import android.net.Uri;
import android.os.AsyncTask;
import android.os.Environment;
import android.util.Log;

public class TaskDownloadNewApp extends AsyncTask<Void, Integer, Void>
{

   ProgressDialog pdialog;
   File file;
   String path;
   Context context;
   public TaskDownloadNewApp(Context _context, String _path)
   {
	  path = _path;
	  context=_context;
   }

   @Override
   protected void onPreExecute()
   {
	   	
	  pdialog = new ProgressDialog(context);
	  pdialog.setCancelable(true);
	  pdialog.setProgressStyle(ProgressDialog.STYLE_HORIZONTAL);
	  pdialog.setMessage("正在下载更新");
	  pdialog.setTitle(null);
	  pdialog.show();
   }

   @Override
   protected Void doInBackground(Void... params)
   {
//    	File pathFile;   	  
   	    URL url;
   	    HttpURLConnection conn;
   	    InputStream is=null;
   		FileOutputStream fos = null;
   		BufferedInputStream bis=null;
   		try
   		{
   		  url = new URL(path);
   		  conn = (HttpURLConnection) url.openConnection();
   		  conn.setConnectTimeout(5000);
   		  // 获取到文件的大小
   	      pdialog.setMax(conn.getContentLength());
   		  is = conn.getInputStream();
      	// 如果相等的话表示当前的sdcard挂载在手机上并且是可用的
     	    if (Environment.getExternalStorageState().equals(Environment.MEDIA_MOUNTED))
     	    {
//     		  pathFile=Environment.getExternalStorageDirectory();
     	      file = new File(Environment.getExternalStorageDirectory(), "YGLogistics.apk");
   		      fos = new FileOutputStream(file);
     	    }
     	    else 
     	    {
//     	      pathFile=Environment.getDataDirectory();
//     	      file=new File("YGLogistics.apk");
     	      fos=new FileOutputStream("YGLogistics.apk");
     	      
     	    }
//     	    Log.i("DEBUG", pathFile.getPath());

   		  bis = new BufferedInputStream(is);
   	      byte[] buffer = new byte[1024];
   		  int len;
   		  int total = 0;
   		  while ((len = bis.read(buffer)) != -1)
   		  {
   		    fos.write(buffer, 0, len);
   			total += len;
   		    // 获取当前下载量
   		    publishProgress(total);
   		    pdialog.setProgress(total);
   		  }
   		 }
   		 catch (MalformedURLException e)
   		 {
   			 
   			e.printStackTrace();
   		 }
   		 catch (IOException e)
   		 {
   			 
   			e.printStackTrace();
   		 }
   		 finally
   		 {
   		   try
   		   {	
   			  if(fos!=null)
   		      {
   			    fos.close();
   			  }
   			  if(bis!=null)
   			  {
   			  	bis.close(); 
   			  }
   			  if(is!=null)
   			  {
   				is.close(); 
   			  }
   		   }
   		   catch (IOException e)
   		   {
   			   
   			  e.printStackTrace();
   		   }
   		 }
	  return null;
   }

   @Override
   protected void onProgressUpdate(Integer... values)
   {
	   
//	  pdialog.setProgress(values[0]);
   }

   @Override
   protected void onPostExecute(Void result)
   {
	   
	  try
	  {
		installApk(file);
	  }
	  catch (Exception e)
	  {
		  
	  }
	  finally
	  {
	    pdialog.dismiss();
	  }
   }

   @Override
   protected void onCancelled()
   {
	   
	  super.onCancelled();
   }

 //安装apk    
   protected void installApk(File file) 
   {  
       Intent intent = new Intent();  
       //执行动作   
       intent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
       intent.setAction(android.content.Intent.ACTION_VIEW); 
       //执行的数据类型   
       intent.setDataAndType(Uri.fromFile(file), "application/vnd.android.package-archive");  
       Activity activity=(Activity)context;
       activity.startActivity(intent);  
   }  
}
